package com.dqp.cslggroup.JWXT;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.bean.course;
import com.dqp.cslggroup.greendao.courseDao;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GetStudentCourse {
    private String url;
    private Map<String, String> course_cookies;
    private int year;
    private int term;
    private static long myCourse = 0;
    private static long couplesCourse = 100;
    private static String Authorization = SharedPreferencesUtil.getStringSp(MyApplication.getContext(), "Authorization", "");

    public GetStudentCourse(String course_url, int year, int term, Map<String, String> cookies, String sno) {
        this.url = course_url;
        this.year = year;
        this.term = term;
        this.course_cookies = cookies;
    }

    public boolean getCourse() {
        try {
            Connection.Response response = Jsoup.connect(url + "/jwglxt/kbcx/xskbcx_cxXsKb.html?gnmkdm=N253508")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("xnm", String.valueOf(year))
                    .data("xqm", String.valueOf(term * term * 3))
                    .cookies(course_cookies)
                    .method(Connection.Method.POST)
                    .header("Authorization", Authorization)
                    .ignoreContentType(true)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                parseCourse(jsonObject);
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public boolean getCouplesCourse() {
        try {
            Connection.Response response = Jsoup.connect(url + "/jwglxt/kbcx/xskbcx_cxXsKb.html?gnmkdm=N253508")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("xnm", String.valueOf(year))
                    .data("xqm", String.valueOf(term * term * 3))
                    .cookies(course_cookies)
                    .header("Authorization", Authorization)
                    .method(Connection.Method.POST)
                    .ignoreContentType(true)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                parseCouplesCourse(jsonObject);
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    static List<course> getCourseList(String course_url, int year, int term, Map<String, String> cookies, String sno) {
        List<course> courseList = new ArrayList<>();
        try {
            Connection.Response response = Jsoup.connect(course_url + "/jwglxt/kbcx/xskbcx_cxXsKb.html?gnmkdm=N253508")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
                    .data("xnm", String.valueOf(year))
                    .data("xqm", String.valueOf(term * term * 3))
                    .cookies(cookies)
                    .header("Authorization", Authorization)
                    .method(Connection.Method.POST)
                    .ignoreContentType(true)
                    .execute();
            if (response.body() != null && response.body().contains("{")) {
                JSONObject jsonObject = JSON.parseObject(response.body());
                courseList = parseCourseList(jsonObject);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return courseList;
    }

    private static void parseCouplesCourse(JSONObject jsonObject) {
        if (jsonObject != null && jsonObject.get("kbList") != null) {
            MyApplication.getDaoInstant().getCourseDao().queryBuilder().where(courseDao.Properties.Who.eq(1)).buildDelete().executeDeleteWithoutDetachingEntities();
            JSONArray timeTable = JSON.parseArray(jsonObject.getString("kbList"));
            for (Object o : timeTable) {
                JSONObject lesson = (JSONObject) o;
                course c = new course();
                c.setXqjmc(lesson.getString("xqjmc"));
                c.setKcmc(lesson.getString("kcmc"));
                c.setXm(lesson.getString("xm"));
                c.setXqmc(lesson.getString("xqmc"));
                c.setCdmc(lesson.getString("cdmc"));
                c.setZcd(lesson.getString("zcd"));
                c.setXf(lesson.getString("xf"));
                c.setKhfsmc(lesson.getString("khfsmc"));
                c.setXnm(lesson.getString("xnm"));
                c.setXqj(lesson.getIntValue("xqj"));
                c.setKcxszc(lesson.getString("kcxszc"));
                c.setWho(1);
                c.setJc(lesson.getString("jc"));
                insertCouplesCourse(c);
            }
        }
    }

    private static void parseCourse(JSONObject jsonObject) {
        if (jsonObject != null && jsonObject.get("kbList") != null) {
            MyApplication.getDaoInstant().getCourseDao().queryBuilder().where(courseDao.Properties.Who.eq(0)).buildDelete().executeDeleteWithoutDetachingEntities();
            JSONArray timeTable = JSON.parseArray(jsonObject.getString("kbList"));
            for (Object o : timeTable) {
                JSONObject lesson = (JSONObject) o;
                course c = new course();
                c.setXqjmc(lesson.getString("xqjmc"));
                c.setKcmc(lesson.getString("kcmc"));
                c.setXm(lesson.getString("xm"));
                c.setXqmc(lesson.getString("xqmc"));
                c.setCdmc(lesson.getString("cdmc"));
                c.setZcd(lesson.getString("zcd"));
                c.setXf(lesson.getString("xf"));
                c.setKhfsmc(lesson.getString("khfsmc"));
                c.setXnm(lesson.getString("xnm"));
                c.setXqj(lesson.getIntValue("xqj"));
                c.setKcxszc(lesson.getString("kcxszc"));
                c.setWho(0);
                c.setJc(lesson.getString("jc"));
                insertMyCourse(c);
            }
        }
    }

    private static void insertMyCourse(course course) {
        String jc = course.getJc();
        if (jc != null && jc.contains(",")) {
            String[] arr = jc.split(",");
            for (String s : arr) {
                course.setJc(s);
                course.setId(myCourse);
                insertMyCourse(course);
            }
        } else MyApplication.getDaoInstant().getCourseDao().insert(course);
        myCourse++;
    }

    private static void insertCouplesCourse(course course) {
        String jc = course.getJc();
        if (jc != null && jc.contains(",")) {
            String[] arr = jc.split(",");
            for (String s : arr) {
                course.setJc(s);
                course.setId(couplesCourse);
                insertCouplesCourse(course);
            }
        } else MyApplication.getDaoInstant().getCourseDao().insert(course);
        couplesCourse++;
    }

    private static List<course> parseCourseList(JSONObject jsonObject) {
        List<course> courseList = new ArrayList<>();
        if (jsonObject != null && jsonObject.get("kbList") != null) {
            JSONArray timeTable = JSON.parseArray(jsonObject.getString("kbList"));
            for (Object o : timeTable) {
                JSONObject lesson = (JSONObject) o;
                course c = new course();
                String jc = lesson.getString("jc");
                c.setXqjmc(lesson.getString("xqjmc"));
                c.setKcmc(lesson.getString("kcmc"));
                c.setXm(lesson.getString("xm"));
                c.setXqmc(lesson.getString("xqmc"));
                c.setCdmc(lesson.getString("cdmc"));
                c.setZcd(lesson.getString("zcd"));
                c.setXf(lesson.getString("xf"));
                c.setKhfsmc(lesson.getString("khfsmc"));
                c.setXnm(lesson.getString("xnm"));
                c.setXqj(lesson.getIntValue("xqj"));
                c.setKcxszc(lesson.getString("kcxszc"));
                c.setJc(jc);
                if (jc != null && jc.contains(",")) {
                    String[] arr = jc.split(",");
                    for (String s : arr) {
                        c.setJc(s);
                        courseList.add(c);
                    }
                } else courseList.add(c);
            }
        }
        return courseList;
    }
}
